/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diagram.handles;

import diagram.Node;
import diagram.connections.Connection;
import diagram.locations.AbsoluteLocation;
import diagram.locations.FlexibleRelativeLocation;
import diagram.locations.VerticalLineLocation;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Arc2D;

/**
 *
 * @author Jelle
 */
public class ConnectHandle extends Handle {

    private Arc2D arc;
    private int lineX,  lineY,  lineX2,  lineY2;
    private Connection connection;
    private boolean isAttached = false;

    public ConnectHandle() {

    }

    public ConnectHandle(Node baseNode) {
        super(baseNode, new VerticalLineLocation(new FlexibleRelativeLocation(baseNode.getLowerRight(), 0, 0 - baseNode.getHeight()), new FlexibleRelativeLocation(baseNode.getLowerRight(), 0, 0 - baseNode.getHeight()), new FlexibleRelativeLocation(baseNode.getLowerRight(), 0, 0)));
        this.lineX = baseNode.getUpperRight().getX();
        this.lineY = baseNode.getUpperRight().getY();
        this.lineX2 = lineX + 100;
        this.lineY2 = lineY;
    }

    public void paint(Graphics2D g) {
        Graphics2D g2 = (Graphics2D) g;
        Color oldColor = g.getColor();
        Stroke s = g.getStroke();
        if (((Node)getBase()).isSelected()) {
            g.setColor(Color.RED);
        } else {
            g.setColor(Color.BLACK);
        }

        g.drawLine(getLocation().getX(), getLocation().getY(), lineX2, lineY2);
        arc = new Arc2D.Double(lineX2, lineY2 - 5, 10, 10, 90, 180, Arc2D.OPEN);
        g2.draw(arc);
        if (this.isHighlighted()) {
            g.setColor(Color.RED);
            g.setStroke(new BasicStroke(2.0f));
            g.drawLine(getLocation().getX(), getLocation().getY(), lineX2, lineY2);
            g2.draw(arc);
        }

        g.setColor(oldColor);
        g.setStroke(s);
    }

    public boolean contains(int x, int y) {
        boolean isIn = false;
        if (x >= getLocation().getX() && y == getLocation().getY() && x <= (getLocation().getX() + 10)) {
            isIn = true;
        }
        if (arc.contains(x, y)) {
            isIn = true;
        }
        return isIn;
    }

    public void AdjustLine(int x, int y) {
        lineX2 = x;
        lineY2 = y;

    }

    public void disableConnection() {
        connection = null;
        isAttached = false;
    }
}
